﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;

using Common;

using Castle.Core.Interceptor;
using Castle.DynamicProxy;

namespace com.globalwave.system
{
    /// <summary>
    /// Organization 的摘要说明
    /// </summary>
    public class OrganizationService : BaseService
    {
        private static NameValueCollection whereMap = null;
        private const string TABLE_NAME = "system_Organization";
        private const string TABLE_ORGANIZATION_PRIVILEGE = "system_Organization_Privilege";
        private const string TABLE_USER_ORGANIZATION = "system_User_Organization" ;

        public OrganizationService()
        {
        }
        public OrganizationService(BaseService other)
            : base(other) 
        {
        }


        virtual public DataSet query(
                NameValueCollection organizationCriterion, 
                int pageIndex, 
                int pageSize) 
        {
            
            /*if (orderList == null) {
                orderList = new ArrayOrderList() ;
                orderList.addAsc("organization_id") ;
            }*/

            return dao.queryTable(TABLE_NAME, pageIndex, pageSize, organizationCriterion, " order by organization_id asc ");
        }


        virtual public int create(NameValueCollection organization, string[] privilege_ids)
        {       
            int result = dao.insertTable(TABLE_NAME, organization) ;

            if (privilege_ids != null)
            {
                NameValueCollection orgPrivilege = new NameValueCollection() ;
                orgPrivilege.Add("organization_id", result.ToString());

                foreach (string privilege_id in privilege_ids) {
                    orgPrivilege.Set("privilege_id", privilege_id);
                    dao.insertTable(TABLE_ORGANIZATION_PRIVILEGE, orgPrivilege) ;
                }
            }
            
            return result;
        }

        virtual public int update(NameValueCollection organization, string[] privilege_ids)
        {

            NameValueCollection criterion = new NameValueCollection();
            string id = organization.Get("organization_id");
            criterion.Add("organization_id", id);

            organization.Remove("organization_id");
            int result = dao.updateTable(TABLE_NAME, organization, criterion) ;
            organization.Add("organization_id", id);

            dao.deleteTable(TABLE_ORGANIZATION_PRIVILEGE, criterion) ;

            if (privilege_ids != null)
            {
                NameValueCollection orgPrivilege = new NameValueCollection() ;
                orgPrivilege.Add("organization_id", id);

                foreach (string privilege_id in privilege_ids) {
                    orgPrivilege.Set("privilege_id", privilege_id);
                    dao.insertTable(TABLE_ORGANIZATION_PRIVILEGE, orgPrivilege) ;
                }
            }

            return result ;       
        }

        virtual public int delete(NameValueCollection organization)
        {
            NameValueCollection criterion = new NameValueCollection() ;
            criterion.Add("organization_id", organization.Get("organization_id"));

            dao.deleteTable(TABLE_ORGANIZATION_PRIVILEGE, criterion) ;
            dao.deleteTable(TABLE_USER_ORGANIZATION, criterion);

            return dao.deleteTable(TABLE_NAME, criterion);
            
        }

        virtual public int deleteAll(string[] organizationIds)
        {
            if (organizationIds == null || organizationIds.Length == 0) {
                throw new BaseException("E000001","参数错误") ;
            }

            NameValueCollection criterion = new NameValueCollection() ;
            foreach(string organizationId in organizationIds) {
                criterion.Add("organization_ids", organizationId) ;
            }
            
            // delete cascade
            dao.deleteTable(TABLE_ORGANIZATION_PRIVILEGE, criterion) ;
            dao.deleteTable(TABLE_USER_ORGANIZATION, criterion);
            
            return dao.deleteTable(TABLE_NAME, criterion) ;
            
        }


        virtual public DataSet userBelongTo(string user_id)
        {

            NameValueCollection criterion = new NameValueCollection();
            criterion.Add("@user_id", user_id);

            return dao.query(OrganizationCriterion.USER_BELONG_TO_ORGANIZATION, -1,-1, criterion, null,null);
        }

        public NameValueCollection getWhereConfigMap()
        {
            if (whereMap != null)
            {
                return whereMap;
            }

            whereMap = new NameValueCollection();

            whereMap.Add("organization_ids", " organization_id in (@organization_ids) ");

            return whereMap;
        }

        public static OrganizationService getInstance()
        {

            ProxyGenerator generator = new ProxyGenerator();

            return generator.CreateClassProxy<OrganizationService>(new TransactionInterceptor());

        }
    }

    public class OrganizationCriterion {
        public const String USER_BELONG_TO_ORGANIZATION = 
            "  select org.organization_id as organization_id, " +
            //"         org.sys_organization_id sys_organization_id, " +
            "         org.pro_organization_id as pro_organization_id, " +
            "         org.name_ as name_, " +
            "         org.desc_ as desc_, " +
            "         uo.user_id as user_id" +
            "    from system_organization org " +
            "         left join (select * from system_user_organization where user_id=?) uo " +
            "                on org.organization_id = uo.organization_id " +
            "order by org.organization_id" ;
        
        
    }
}
